#include "deg2_rad_custom_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <iostream>
#include <algorithm>

#include "register/tilingdata_base.h"
#include "tiling/tiling_api.h"

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context) {
    Deg2RadCustomTilingData tiling;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto num_cores = ascendcPlatform.GetCoreNum();

    const uint32_t alignment = 64;

    uint32_t size =  context->GetInputShape(0)->GetOriginShape().GetDim(0);
    tiling.set_size(size);      //Element num

    unsigned length = (size - 1) / num_cores + 1;

    length += length % alignment;

    tiling.set_length(length);
    
    context->SetBlockDim(num_cores);
    
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
    context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;

    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Deg2RadCustom : public OpDef {
public:
    explicit Deg2RadCustom(const char* name) : OpDef(name)
    {
        this->Input("input")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Output("out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);
        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910")
                        .AddConfig("ascend310p")
                        .AddConfig("ascend310b")
                        .AddConfig("ascend910b");

    }
};

OP_ADD(Deg2RadCustom);
}
